package com.yk.anima.config;


import com.yk.anima.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author xiaokai
 * @create 2021-05-12 10:15:42
 * @desc...自定义拦截器的配置，实现WebMvcConfigurer不会导致Spring Boot对mvc的自动配置失效
 */
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {

    //静下来想一想,拦截器加载的时间点在springcontext之前,即在bean实例化之前,所以在拦截器中注入自然为null.
    //那么我们就让拦截器执行的时候实例化拦截器Bean,在拦截器配置类里面先实例化拦截器,然后再获取就能解决这个问题啦
    @Bean
    public MyInterceptor getMyInterceptor() {
        return new MyInterceptor();

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 实现WebMvcConfigurer不会导致静态资源被拦截
//        registry.addInterceptor(new MyInterceptor())
        registry.addInterceptor(getMyInterceptor())
                // 拦截所有url
                .addPathPatterns("/**")
                // 放行swagger
                .excludePathPatterns("/swagger-resources/**")
                .excludePathPatterns("/swagger-ui.html/**")
                // 放行/user
                .excludePathPatterns("/user/**")
                .excludePathPatterns("/webjars/**/**");



    }



}